home *** CD-ROM | disk | FTP | other *** search
- Path: qcd.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: Is this ok: *pointer++ = value ??
- Date: 6 Jan 1996 19:12:31 GMT
- Organization: Los Alamos National Laboratory
- Distribution: world
- Message-ID: <4cmhj0$irn@newshost.lanl.gov>
- References: <4cklvv$nmm@alcor.usc.edu>
- NNTP-Posting-Host: qcd.lanl.gov
-
- In article <4cklvv$nmm@alcor.usc.edu>, wawda@alcor.usc.edu (Abu Wawda)
- writes:
- |> I tried the following on my C compiler and it worked as expected:
- |>
- |> int i;
- |> char *pointer;
- |>
- |> pointer = (char *) malloc(50);
- |> for (i=0; i<50; i++) *pointer++ = i;
- |>
- |> My only problem is why? I mean you aren't allowed to do:
- |>
- |> int i,p;
- |>
- |> for (i=0; i<50; i++) p++ = i;
- |>
- |> Like you aren't supposed to be put anything like x+5 or some other
- |> unsolved quantity on the left side for assignment. However, is this an
-
- Hi Abu,
-
- Your problem is that you are trying to oversimplify. I will try to address
- what I think is the basic misunderstanding in your statement: you think of
- `x+5' as an `unsolved' quantity. There is no such concept in C.
-
- In C, there are identifiers (i.e. names, like `x') which start of
- standing for for objects. Thus, when one writes &x one is talking
- about the address of x (naturally, this address is not an object). A point
- to be noted, though irrelevant to our discussion, is that
- not every `object' has an address: register objects have no address, and
- functions are not objects but have addresses.
-
- Similarly when one says x = 5, one wants to store 5 into the object x. 5 = 3
- is however meaningless because it wants to store 3 into the `5', but `5' is
- not an object.
-
- However, one can also use x in a slightly different way. When one writes x +
- 3 or y = x, one is using x as a value. In C, there are general rules of how
- to determine the `value' (sometimes called `rvalue') of an object (often
- called `lvalue'): for non-array objects, it is the value last stored in the
- object. Again one sees how to interpret x+3: the `values' of x and 3 are
- added to produce a new value. Thus x+3 = 5 does not make sense either: this
- would try to store 5 into x+3, which is a value and not an object.
-
- The ++ operator is somewhat special. It treats its operand both as a value
- and as an object. Thus, when one writes p++, the `object p' gets the value
- which is one more than the original `value p'. In addition, `p++' itself can
- work as a value: the value of `p++' is the original value of p (i.e. the
- value before the new value was stored). But note that p++ is not itself an
- object: it is merely a value, and hence p++ = i is wrong.
-
- On the other hand, pointers are objects that store addresses of other
- objects. Thus the value of a pointer object is the address of another object.
- Applying the * operator to it produces this object. Thus, when you write
- *pointer++, the compiler parses (`understands') the expression as follows:
-
- "Take the value of the object pointer, add one to it, and store the result
- into the object called pointer (this is the side effect of the ++ operator
- mentioned above).
-
- The expression pointer++ however stands for the unincremented value of the
- object pointer: the object that this value points to is the object that
- *pointer++ stands for".
-
- Again, as a side note, one must point out that when exactly the side effect
- (i.e. changing the object p) takes place is not specified by the language:
- the programmer should only assume that it happens by the next sequence point.
- The FAQ explains where the sequence points are: in your example, there is one
- at the end of the statement.
-
- Thus, even though pointer++ is not an object, *pointer++ is. This object may
- in turn be converted to a value if required (as in the expression *pointer++
- + 3). However, it may naturally be used directly as an object: *pointer++ =
- 3.
-
- Does this clarify?
-
- |> exception for pointers? I can understand doing it backwards as
- |> perfectly legal:
- |>
- |> int x;
- |>
- |> x = *pointer++;
- |>
- |> but not:
- |>
- |> *pointer++ = x;
- |>
- |> I would appreciate any hints. Thanks you.
-
- Both are legal (provided of course that pointer points to an object of
- appropriate type). As you can see the exception is not for pointers per se;
- in C however, a non-trivial expression stands for an object only when dealing
- with pointers. C++ (a related, but completely different language) rules are
- different: many more expressions (most notably ++p etc.) stand for objects
- there.
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-